#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare_int(const void* e1, const void* e2)
{
	if (*(int*)e1 > *(int*)e2)
	{
		return 1;
	}
	else if (*(int*)e1 < *(int*)e2)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

void print(int arr[], int sz)
{
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void test1(void)
{
	int arr[10] = { 9,1,8,7,6,4,5,3,2,0 };
	int len = sizeof(arr) / sizeof(arr[0]);

	qsort(arr, len, sizeof(arr[0]), compare_int);
	print(arr, len);
}


struct Stu
{
	char name[20];
	int age;
}stu;

int com_stu_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct Stu*)e1)->name, ((struct Stu *)e2)->name);
}

int com_stu_by_age(const void* e1, const void* e2)
{
	return ((((struct Stu*)e1)->age) - (((struct Stu*)e2)->age));
}


//
//void print(struct stu arr[], int len)
//{
//	for (int i = 0; i < len; i++)
//	{
//		printf("%s %d\n", arr[i].name, arr[i].age);
//	}
//}

void test2(void)
{
	struct Stu arr[3] = { { "zhangsan", 20 },{ "lisi", 30 },{ "wangwu", 5 } };
	int len = sizeof(arr) / sizeof(arr[0]);

	qsort(arr, len, sizeof(arr[0]), com_stu_by_name);

	for (int i = 0; i < len; i++)
	{
		printf("%s %d\n", arr[i].name, arr[i].age);
	}
}

int main()
{
	//test1();

	test2();

	return 0;
}